package com.xmtlxx.common.sms.aliyun;

import com.xmtlxx.common.config.XmtlxxConfig;
import com.xmtlxx.common.sms.aliyun.config.AliyunConfig;
import com.xmtlxx.common.utils.JsonTools;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.wxap.util.HttpInvokerUtils;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class AliyunSmsService {

    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            // 必填，您的 AccessKey ID
            .setAccessKeyId(accessKeyId)
            // 必填，您的 AccessKey Secret
            .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = XmtlxxConfig.getAliyunSmsEndpoint();
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 阿里云短信单次发送
     */
    @Async
    public void AliyunSmsSend(SmsRequest smsRequest){
        try {
            if(XmtlxxConfig.getDebugEvn()){
                smsRequest.setPhoneNumber(XmtlxxConfig.getDebugPhone());
            }
            Map<String,Object> data = new HashMap<>();
            data.put("api","sms");
            data.put("data",smsRequest);
            String result = HttpInvokerUtils.PostToJson(XmtlxxConfig.getProtocol()+"://"+ XmtlxxConfig.getAliyunSmsEndpoint(), JsonUtils.toJsonString(data));
            System.out.println("AliyunSmsSend===>"+result);
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    @Async
    public void AliyunSmsSendTest(SmsRequest smsRequest){
        try {
            com.aliyun.dysmsapi20170525.Client client = AliyunSmsService.createClient(AliyunConfig.accessKeyId, AliyunConfig.accessKeySecret);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
            sendSmsRequest.setSignName(AliyunConfig.signName);
            if(XmtlxxConfig.getDebugEvn()){
                sendSmsRequest.setPhoneNumbers(XmtlxxConfig.getDebugPhone());
            }else {
                sendSmsRequest.setPhoneNumbers(smsRequest.getPhoneNumber());
            }

            sendSmsRequest.setTemplateCode(smsRequest.getTemplateCode());
            sendSmsRequest.setTemplateParam(JsonTools.getJsonString4JavaPOJO(smsRequest.getTemplateParam()));
            com.aliyun.dysmsapi20170525.models.SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
            System.out.println(new Gson().toJson(sendResp));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 阿里云短信批量发送
     */
    @Async
    public void AliyunSmsSendBatch(SmsBatchRequest batchRequest){
        try {
            com.aliyun.dysmsapi20170525.Client client = AliyunSmsService.createClient(AliyunConfig.accessKeyId, AliyunConfig.accessKeySecret);
            com.aliyun.dysmsapi20170525.models.SendBatchCardSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendBatchCardSmsRequest();

            if(XmtlxxConfig.getDebugEvn()){
                List<String> phones = new ArrayList<>();
                int nums = batchRequest.getPhoneNumberJson().size();
                for(int i = 0;i<nums;i++){
                    phones.add(XmtlxxConfig.getDebugPhone());
                }
                sendSmsRequest.setPhoneNumberJson(JsonTools.getJsonString4List(phones));
            }else {
                sendSmsRequest.setPhoneNumberJson(JsonTools.getJsonString4List(batchRequest.getPhoneNumberJson()));
            }

            sendSmsRequest.setSignNameJson(JsonTools.getJsonString4List(batchRequest.getSignNameJson()));
            sendSmsRequest.setTemplateCode(batchRequest.getTemplateCode());
            sendSmsRequest.setTemplateParamJson(JsonTools.getJsonString4List(batchRequest.getTemplateParamJson()));

            com.aliyun.dysmsapi20170525.models.SendBatchCardSmsResponse resp = client.sendBatchCardSms(sendSmsRequest);
            System.out.println(new Gson().toJson(resp));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async
    public void testSendSMS(){
        SmsRequest smsRequest = new SmsRequest();
        smsRequest.setPhoneNumber(XmtlxxConfig.getDebugPhone());
        smsRequest.setSignName(AliyunConfig.signName);
        smsRequest.setTemplateCode(SmsTemplateCode.TASK_NOTICE.getSMS_CODE());
        SmsTemplateParam templateParam = new SmsTemplateParam();
        templateParam.setOrderType("入库");
        smsRequest.setTemplateParam(templateParam);

        AliyunSmsSend(smsRequest);
    }

}
